package com.jielihaofeng.nio.filechannel;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @description 遍历目录文件
 * @author Johnnie Wind
 * @date 2021/10/10 15:47
 */
public class TestFilesWalkFileTree {

    public static void main(String[] args) throws IOException {
        // 遍历JDK目录
//        walkJdkDirector();
        // 遍历JDK目录下的Jar文件
//        walkJars();
        // 删除文件和目录
//        deleteFileAndDirector();
    }

    /**
     * @description 删除文件和目录【】
     * @author Johnnie Wind
     * @date 2021/10/10 16:10
     * @Param []
     * @return void
     */
    private static void deleteFileAndDirector() throws IOException {
        Files.walkFileTree(Paths.get("C:\\Users\\伍政宇\\Downloads"), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                System.out.println("====> 进入" + dir);
                return super.preVisitDirectory(dir, attrs);
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println(file);
                Files.delete(file);
                return super.visitFile(file, attrs);
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                System.out.println("<==== 退出" + dir);
                Files.delete(dir);
                return super.postVisitDirectory(dir, exc);
            }
        });
    }

    /**
     * @description 遍历jar包
     * @author Johnnie Wind
     * @date 2021/10/10 16:02
     * @Param []
     * @return void
     */
    private static void walkJars() throws IOException {
        final AtomicInteger jarCount = new AtomicInteger();
        Files.walkFileTree(Paths.get("C:\\Program Files\\Java\\jdk1.8.0_231"), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (file.toString().endsWith(".jar")) {
                    System.out.println(file);
                    jarCount.incrementAndGet();
                }
                return super.visitFile(file, attrs);
            }
        });
        System.out.println("jar count:" + jarCount);
    }

    /**
     * @description 遍历JDK目录
     * @author Johnnie Wind
     * @date 2021/10/10 15:56
     * @Param []
     * @return void
     */
    private static void walkJdkDirector() throws IOException {
        final AtomicInteger dirCount = new AtomicInteger();
        final AtomicInteger fileCount = new AtomicInteger();

        // 用到设计模式：访问者模式
        // jdk目录：C:\Program Files\Java\jdk1.8.0_231
        Files.walkFileTree(Paths.get("C:\\Program Files\\Java\\jdk1.8.0_231"), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                System.out.println("====>" + dir);
                dirCount.incrementAndGet();
                return super.preVisitDirectory(dir, attrs);
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                System.out.println(file);
                fileCount.incrementAndGet();
                return super.visitFile(file, attrs);
            }
        });

        System.out.println("dir count:" + dirCount);
        System.out.println("file count:" + fileCount);
    }
}
